import datetime

from django.shortcuts import render

# Create your views here.
from rest_framework import viewsets, status
from rest_framework.response import Response
from rest_framework.generics import DestroyAPIView
from rest_framework.mixins import CreateModelMixin, DestroyModelMixin, RetrieveModelMixin, ListModelMixin
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from rest_framework.authentication import SessionAuthentication
from rest_framework.permissions import IsAuthenticated
from user_operation.permissions import IsOwnerOrReadOnly
from .serializers import ShoppingCartSerializer, ShoppingCartDetialSerializer, \
                            OrderInfoSerializer, OrderInfoDetailSerializer
from .models import ShoppingCart, OrderInfo, OrderGoods


class ShoppingCartViewSet(viewsets.ModelViewSet):
    """
        list:
            得到该登录用户购物车的信息
        create:
            将商品加入购物车
            前端传过来的参数：{
                goods: this.productId, // 商品id
                nums: this.buyNum, // 购买数量
            }
        destroy:
            删除购物车的商品
        update:
            更新购物车商品的数量
    """
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)
    permission_classes = (IsAuthenticated, IsOwnerOrReadOnly)
    lookup_field = "goods_id"
    serializer_class = ShoppingCartSerializer

    def get_queryset(self):
        """
        限制获得当前用户的查询集
        :return:
        """
        return ShoppingCart.objects.filter(user=self.request.user)

    def get_serializer_class(self):
        """
        当action为list要getShopCarts获得购物车所有的商品的时候，就是跳转shopcart组件的时候了，这时候需要对goods字段进行详细的展示
        所以需要序列化商品字段
        :return:
        """
        if self.action == "list":
            return ShoppingCartDetialSerializer
        else:
            return ShoppingCartSerializer




class ShoppingCartDeleteAll(DestroyAPIView):
    """
    删除购物车的所有商品
    """
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)
    permission_classes = (IsAuthenticated, IsOwnerOrReadOnly)
    serializer_class = ShoppingCartDetialSerializer

    def get_queryset(self):
        return ShoppingCart.objects.filter(user=self.request.user)

    def destroy(self, request, *args, **kwargs):
        queryset = self.get_queryset()
        queryset.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)


class OrderInfoViewSet(ListModelMixin, DestroyModelMixin, CreateModelMixin, RetrieveModelMixin,
                       viewsets.GenericViewSet):

    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)
    permission_classes = (IsAuthenticated, IsOwnerOrReadOnly)
    serializer_class = OrderInfoSerializer

    def get_queryset(self):
        return OrderInfo.objects.filter(user=self.request.user)

    def perform_create(self, serializer):
        order = serializer.save()
        shopcart = ShoppingCart.objects.filter(user=self.request.user)
        for goods in shopcart:
            order_goods = OrderGoods()
            order_goods.order = order
            order_goods.goods = goods.goods
            order_goods.goods_num = goods.nums
            order_goods.save()
        shopcart.delete()
        return order

    def get_serializer_class(self):
        if self.action == "retrieve":
            return OrderInfoDetailSerializer
        else:
            return OrderInfoSerializer

from rest_framework.views import APIView
from rest_framework.response import Response
from django.shortcuts import redirect
from utils.alipy import AliPay
from FoodMarket.settings import APP_PRIVATE_KEY_PATH, ALIPY_PUBLIC_KEY_PATH
from trade.models import OrderInfo


class AlipyViewSet(APIView):
    def get(self, request):
        """
        处理支付宝的return_url
        :param request:
        :return:
        """
        processed_query = {}
        for key, value in request.GET.items():
            processed_query[key] = value
        ali_sign = processed_query.pop("sign", None)
        alipay = AliPay(
            appid="2016091700535393",
            app_notify_url="http://122.114.105.49:8000/alipy/return",
            app_private_key_path=APP_PRIVATE_KEY_PATH,
            alipay_public_key_path=ALIPY_PUBLIC_KEY_PATH,  # 支付宝的公钥，验证支付宝回传消息使用，不是你自己的公钥,
            debug=True,  # 默认False,
            return_url="http://122.114.105.49:8000/alipy/return"
        )
        if alipay.verify(processed_query, ali_sign):
            order_sn = processed_query.get("out_trade_no")
            trade_no = processed_query.get("trade_no")
            pay_status = "TRADE_SUCCESS"
            order_list = OrderInfo.objects.filter(order_sn=order_sn)
            if order_list.count() == 0 or order_list.count() >= 2:
                return Response("fail")
            for order in order_list:
                order.trade_no = trade_no
                order.pay_status = pay_status
                order.save()
            response = redirect("index")
            response.set_cookie("nextPath", "pay", max_age=2)
            return response
        else:
            response = redirect("index")
            return response

    def post(self, request):
        """
        处理支付宝的notify_url
        :param request:
        :return:
        """
        processed_query = {}
        for key, value in request.POST.items():
            processed_query[key] = value
        ali_sign = processed_query.pop("sign", None)
        alipay = AliPay(
            appid="2016091700535393",
            app_notify_url="http://122.114.105.49:8000/alipy/return",
            app_private_key_path=APP_PRIVATE_KEY_PATH,
            alipay_public_key_path=ALIPY_PUBLIC_KEY_PATH,  # 支付宝的公钥，验证支付宝回传消息使用，不是你自己的公钥,
            debug=True,  # 默认False,
            return_url="http://122.114.105.49:8000/alipy/return"
        )
        if alipay.verify(processed_query, ali_sign):
            order_sn = processed_query.get("out_trade_no", None)
            trade_no = processed_query.get("trade_no", None)
            pay_status = processed_query.get("trade_status", None)
            order_list = OrderInfo.objects.filter(order_sn=order_sn)
            if order_list.count() == 0 or order_list.count() >= 2:
                return Response("fail")
            for order in order_list:
                order.trade_no = trade_no
                order.pay_status = pay_status
                order.pay_time = datetime.datetime.now()
                order.save()
            return Response("success")
        else:
            return Response("fail")

